package emotion_codesoe_com

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/emotion_codesoe_com"
	emotion_codesoe_comReq "github.com/flipped-aurora/gin-vue-admin/server/model/emotion_codesoe_com/request"
)

type OsMenuService struct {
}

// CreateOsMenu 创建osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) CreateOsMenu(osMenu *emotion_codesoe_com.OsMenu) (err error) {
	err = global.GVA_DB.Create(osMenu).Error
	return err
}

// DeleteOsMenu 删除osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) DeleteOsMenu(id string) (err error) {
	err = global.GVA_DB.Delete(&emotion_codesoe_com.OsMenu{}, "id = ?", id).Error
	return err
}

// DeleteOsMenuByIds 批量删除osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) DeleteOsMenuByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]emotion_codesoe_com.OsMenu{}, "id in ?", ids).Error
	return err
}

// UpdateOsMenu 更新osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) UpdateOsMenu(osMenu emotion_codesoe_com.OsMenu) (err error) {
	err = global.GVA_DB.Save(&osMenu).Error
	return err
}

// GetOsMenu 根据id获取osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) GetOsMenu(id string) (osMenu emotion_codesoe_com.OsMenu, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&osMenu).Error
	return
}

// GetOsMenuInfoList 分页获取osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) GetOsMenuInfoListPage(info emotion_codesoe_comReq.OsMenuSearch) (list []emotion_codesoe_com.OsMenu, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&emotion_codesoe_com.OsMenu{})
	var osMenus []emotion_codesoe_com.OsMenu
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.SkipMode != nil {
		db = db.Where("skip_mode = ?", info.SkipMode)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&osMenus).Error
	return osMenus, total, err
}

// GetOsMenuInfoList 分页获取osMenu表记录
// Author [piexlmax](https://github.com/piexlmax)
func (osMenuService *OsMenuService) GetOsMenuInfoList(info emotion_codesoe_comReq.OsMenuSearch) (list []emotion_codesoe_com.OsMenu, err error) {

	// 创建db
	db := global.GVA_DB.Model(&emotion_codesoe_com.OsMenu{})
	var osMenus []emotion_codesoe_com.OsMenu
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.SkipMode != nil {
		db = db.Where("skip_mode = ?", info.SkipMode)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}

	if err != nil {
		return
	}

	err = db.Order("sort DESC").Find(&osMenus).Error
	return osMenus, err
}
